1. Field of the Invention
The present invention relates to a data processing apparatus, method and computer program product for reducing memory usage of an object oriented program.
2. Description of the Prior Art
In object oriented programming (OOP) systems, a “class” (also referred to herein as a class definition) can be used to provide a template for the creation of “objects” having characteristics of that class. Objects are typically created dynamically (in the Java programming language, using an operator called the “new” operator) during program execution. The class definition will typically provide a number of functions (also referred to herein as “methods”) that can operate on objects created from that class definition. Further, each class definition will typically identify a number of storage items to be provided in memory for each object created from that class definition, these storage items being used to store data values associated with the object. One example of such storage items are the fields that are typically defined by a class definition. Each time an object is created from the class definition, those fields will be replicated within the memory associated with that object, and particular values given to those fields. Considering a simple example, a class definition may be established for a bicycle, and a number of fields may be defined for that class, for example cadence, speed, number of gears, etc. When each object is created from that class definition, values of cadence, speed, gear, etc will be instantiated as fields within the object.
As another example of storage items that can be identified by the class definition, each of the methods defined by the class definition will typically require one or more temporary variables when executing, and these temporary variables are also storage items that need to be provided in memory for each executing method.
Modern day object oriented programs may create many objects during program execution, that each use a certain amount of space in memory. Typically the memory space allocated to the program is a scarce resource and needs to be used as efficiently as possible. The objects are sometimes quite dynamic and, as mentioned earlier, typically have a set of fields within them. Instances of these fields can be very numerous, either as separate data entities, or as array members. Further, as mentioned earlier, storage items also need to be provided in memory for any methods executing, so as to provide locations in which to store temporary variables used by those methods. Hence, it will be appreciated that the amount of memory that needs to be allocated to storage items such as the fields associated with each object, or the temporary variables associated with each executing method, becomes significant.
Generally, the instructions constituting the object oriented program will not run directly on the underlying hardware, and instead a virtual machine (VM) is provided between the object oriented program and the underlying hardware to convert the OOP instructions into instructions that can be executed on the underlying hardware. Typically the virtual machine is responsible for generating objects from the associated class definitions and allocating space in memory for each such object, including space for the fields of the object. The virtual machine also typically provides space in memory for each executing method, including space for holding temporary variables for use when executing. One example of such a virtual machine is the Java virtual machine (JVM) used to execute Java programs.
In modern day systems, it is becoming more commonplace for the virtual machine to be able to support multiple programs, and this further increases the importance of managing the space that each object uses in memory.
Accordingly, it would be desirable to provide a technique for reducing memory usage of an object oriented program.
In the area of Web page design, a programming language called PHP has been developed for producing dynamic Web pages. PHP does not mandate any typing rules from the programmer and will use the appropriate data type conversions in the context of the specified operations. As a result, the type associated with a variable can be changed at runtime, and accordingly in some instances the data constituting that variable may be considered to be a string, whilst in other situations it may be considered to be an integer. However, the size of the variables is not varied. With regard to the issue of reducing memory usage, PHP preserves memory by using dynamic and sparse arrays to enable the amount of data to increase as needed, but the data type sizes are fixed.
In the area of compiler technology, some C compilers choose the enumerated type variable width based upon the largest enum value used. Hence, the compiler can decide whether to allocate an enum as an integer, a byte, a character, etc. However, this is not performed at runtime, but instead is conformed at compilation time. Hence, such a technique does not allow a variable's size to be dynamically changed once the machine is running.
Whilst the above-described techniques allow some manipulation of data type to take place, they do not provide any mechanisms for reducing memory usage of an object oriented program, and in particular for reducing the amount of memory allocated to storage items to be provided in memory for each object to store data values associated with the object.